package com.rg1906.web;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.rg1906.service.impl.InsertTeacherServiceImpl;


@WebServlet("/InsertTeacher")
public class InsertTeacher extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	InsertTeacherServiceImpl service=new InsertTeacherServiceImpl();
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String teacherName = request.getParameter("teacherName");
		String gid = request.getParameter("gid");
		String phone=request.getParameter("phone");
		String address=request.getParameter("address");
		String sex=request.getParameter("sex");
		String age=request.getParameter("age");
		String img="static/images/3.jpg";
		try {
			String SNum=null;
			String maxNum=null;
			int ssNum=0;
			String sNum=null;
			 maxNum = service.getMaxNum(gid);
			if(maxNum==null) {
			maxNum="T"+gid+"000"+1;	
			}else {
				String[] split = maxNum.split("T");
				 SNum = split[1];//分割得来的学号加1
				 ssNum = Integer.parseInt(SNum)+1;
				 sNum="T"+ssNum;//学号
			}
			int i = service.insertTeacher(teacherName,gid,phone,address,sex,age,sNum);
			//把工号添加到user表中rid=3;
			int ii = service.insertTeacherSNum(sNum,3,img);
			//查找当登录名为sNum时的id
			int gid1 = service.queryID(sNum);
			int iii = service.insertUid(gid1,sNum);
			if(i>0&&ii>0&&iii>0) {
				response.getWriter().print(true);
			}else {
				response.getWriter().print(false);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
